using Circus.CodeEditor.TextSource;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
namespace Circus.CodeEditor.Dialogs
{
	public class SearchDialog  
	{
		private static DlgSearch searchDlg = null;
		private static SearchSettings searchSettings = new SearchSettings();
		private Font font;
		private static Form ownerForm;
		private static bool regularExpressionsVisible = true;
		private static bool regularExpressionsEnabled = true;
	    private event HelpEventHandler helpRequested;

	    public virtual event HelpEventHandler HelpRequested;
		public bool Visible
		{
			get
			{
				return SearchDialog.searchDlg != null && SearchDialog.searchDlg.Visible;
			}
			set
			{
				if (SearchDialog.searchDlg != null && SearchDialog.searchDlg.Visible != value)
				{
					SearchDialog.searchDlg.Visible = value;
					this.OnVisibleChanged();
				}
			}
		}
		public Form OwnerForm
		{
			get
			{
				return (SearchDialog.searchDlg != null) ? SearchDialog.searchDlg.Owner : SearchDialog.ownerForm;
			}
			set
			{
				if (SearchDialog.ownerForm != value)
				{
					SearchDialog.ownerForm = value;
					this.OnOnwerFormChanged();
				}
			}
		}
		public Font Font
		{
			get
			{
				return this.font;
			}
			set
			{
				if (this.font != value)
				{
					this.font = value;
					this.OnFontChanged();
				}
			}
		}
		public virtual SearchSettings SearchSettings
		{
			get
			{
				return SearchDialog.searchSettings;
			}
		}
		public virtual DlgSearch SearchDlg
		{
			get
			{
				return SearchDialog.searchDlg;
			}
		}
		private void CopyList(IList fromList, IList<string> toList)
		{
			toList.Clear();
			foreach (object current in fromList)
			{
				toList.Add(current.ToString());
			}
		}
		private void CopyList(IList<string> fromList, IList toList)
		{
			toList.Clear();
			foreach (string current in fromList)
			{
				toList.Add(current);
			}
		}
		private void CheckOption(SearchOptions value)
		{
			if ((SearchDialog.searchSettings.SearchOptions & value) == SearchOptions.None)
			{
				SearchDialog.searchSettings.SearchOptions = (SearchDialog.searchSettings.SearchOptions | value);
			}
			else
			{
				SearchDialog.searchSettings.SearchOptions = (SearchDialog.searchSettings.SearchOptions & ~value);
			}
		}
		private void DoClose(object Sender, EventArgs e)
		{
			if (SearchDialog.searchDlg != null)
			{
				SearchDialog.searchSettings.SearchOptions = SearchDialog.searchDlg.Options;
				SearchDialog.searchSettings.ClearBookmarks = SearchDialog.searchDlg.ClearBookmarks;
				SearchDialog.searchSettings.OptionsVisible = SearchDialog.searchDlg.OptionsVisible;
				this.CopyList(SearchDialog.searchDlg.cbFindWhat.Items, SearchDialog.searchSettings.SearchList);
				this.CopyList(SearchDialog.searchDlg.cbReplaceWith.Items, SearchDialog.searchSettings.ReplaceList);
			}
			SearchDialog.searchDlg = null;
		}
		private void DoHelpEvent(object sender, HelpEventArgs e)
		{
			if (this.helpRequested != null)
			{
				this.helpRequested(this, e);
			}
		}
		protected virtual void OnVisibleChanged()
		{
		}
		protected virtual void OnOnwerFormChanged()
		{
			if (SearchDialog.searchDlg != null)
			{
				SearchDialog.searchDlg.Owner = SearchDialog.ownerForm;
			}
		}
		protected virtual void OnFontChanged()
		{
			if (SearchDialog.searchDlg != null)
			{
				if (this.font == null)
				{
					SearchDialog.searchDlg.ResetFont();
				}
				else
				{
					SearchDialog.searchDlg.Font = this.font;
				}
			}
		}
		protected virtual DlgSearch CreateSearchDlg()
		{
			return new DlgSearch();
		}
		public virtual DialogResult Execute(CodeEditor search, bool isModal, bool isReplace)
		{
			return this.Execute(search, isModal, isReplace, null);
		}
		public virtual DialogResult Execute(CodeEditor search, bool isModal, bool isReplace, IWin32Window owner)
		{
			string text = string.Empty;
			bool flag = search.CanSearchSelection(out text) && text.Trim() != string.Empty;
			bool flag2 = flag && StringItem.Split(text).Length <= 1;
			SearchOptions searchOptions = SearchOptions.None;
			if (SearchDialog.searchDlg == null)
			{
				SearchDialog.searchDlg = this.CreateSearchDlg();
				SearchDialog.searchDlg.HelpRequested += new HelpEventHandler(this.DoHelpEvent);
				searchOptions = SearchDialog.searchSettings.SearchOptions;
				SearchDialog.searchDlg.ClearBookmarks = SearchDialog.searchSettings.ClearBookmarks;
				if (flag && (!flag2 || (SearchDialog.searchSettings.SearchOptions & SearchOptions.FindSelectedText) == SearchOptions.None))
				{
					searchOptions |= SearchOptions.SelectionOnly;
				}
				else
				{
					searchOptions &= ~SearchOptions.SelectionOnly;
				}
				SearchDialog.searchDlg.OptionsVisible = SearchDialog.searchSettings.OptionsVisible;
				this.ShowRegularExpressions(SearchDialog.regularExpressionsVisible);
				this.EnableRegularExpressions(SearchDialog.regularExpressionsEnabled);
				this.CopyList(SearchDialog.searchSettings.SearchList, SearchDialog.searchDlg.cbFindWhat.Items);
				this.CopyList(SearchDialog.searchSettings.ReplaceList, SearchDialog.searchDlg.cbReplaceWith.Items);
				SearchDialog.searchDlg.Closed += new EventHandler(this.DoClose);
			}
			if (flag)
			{
				text = text.Trim();
				if (flag2 && (SearchDialog.searchSettings.SearchOptions & SearchOptions.FindSelectedText) != SearchOptions.None)
				{
					if (text != string.Empty)
					{
						SearchDialog.searchDlg.cbFindWhat.Text = text;
					}
				}
				else
				{
					text = string.Empty;
				}
			}
			if ((SearchDialog.searchSettings.SearchOptions & SearchOptions.FindTextAtCursor) != SearchOptions.None && text == string.Empty)
			{
				text = search.GetTextToSearchAtCursor().Trim();
				if (text != string.Empty)
				{
					SearchDialog.searchDlg.cbFindWhat.Text = text;
				}
			}
			SearchDialog.searchDlg.Search = search;
			SearchDialog.searchDlg.IsReplace = isReplace;
			SearchDialog.searchDlg.SelectionEnabled = flag;
			if (this.font != null)
			{
				SearchDialog.searchDlg.Font = this.font;
			}
			SearchDialog.searchDlg.Init();
			SearchDialog.searchDlg.Options = searchOptions;
			DialogResult result;
			if (isModal)
			{
				Form form = SearchDialog.searchDlg;
				DialogResult dialogResult = (owner != null) ? SearchDialog.searchDlg.ShowDialog(owner) : SearchDialog.searchDlg.ShowDialog();
				form.Dispose();
				result = dialogResult;
			}
			else
			{
				if (SearchDialog.ownerForm == null)
				{
					SearchDialog.searchDlg.TopMost = true;
					SearchDialog.searchDlg.Owner = null;
				}
				else
				{
					SearchDialog.searchDlg.TopMost = false;
					SearchDialog.searchDlg.Owner = SearchDialog.ownerForm;
				}
				SearchDialog.searchDlg.Show();
				result = DialogResult.None;
			}
			return result;
		}
		public static void Done(CodeEditor search)
		{
			if (SearchDialog.searchDlg != null && SearchDialog.searchDlg.Search == search)
			{
				SearchDialog.searchDlg.Search = null;
			}
		}
		public virtual void DoneSearch(CodeEditor search)
		{
			if (SearchDialog.searchDlg != null && SearchDialog.searchDlg.Search == search)
			{
				SearchDialog.searchDlg.Search = null;
				SearchDialog.searchDlg.Close();
			}
		}
		public virtual void Close()
		{
			if (SearchDialog.searchDlg != null)
			{
				SearchDialog.searchDlg.Close();
			}
		}
		public virtual void EnsureVisible(Rectangle rect)
		{
			if (SearchDialog.searchDlg != null)
			{
				Rectangle rectangle = rect;
				rectangle.Intersect(SearchDialog.searchDlg.Bounds);
				if (!rectangle.IsEmpty)
				{
					if (rect.Bottom + SearchDialog.searchDlg.Height < SystemInformation.WorkingArea.Height)
					{
						SearchDialog.searchDlg.Top = rect.Bottom;
					}
					else
					{
						SearchDialog.searchDlg.Top = Math.Max(rect.Top - SearchDialog.searchDlg.Height, 0);
					}
					if (rect.Right + SearchDialog.searchDlg.Width < SystemInformation.WorkingArea.Width)
					{
						SearchDialog.searchDlg.Left = rect.Right;
					}
					else
					{
						SearchDialog.searchDlg.Left = Math.Max(rect.Left - SearchDialog.searchDlg.Width, 0);
					}
				}
			}
		}
		public virtual void ToggleHiddenText()
		{
			this.CheckOption(SearchOptions.SearchHiddenText);
			if (SearchDialog.searchDlg != null)
			{
				SearchDialog.searchDlg.chbSearchHiddenText.Checked = !SearchDialog.searchDlg.chbSearchHiddenText.Checked;
			}
		}
		public virtual void ToggleMatchCase()
		{
			this.CheckOption(SearchOptions.CaseSensitive);
			if (SearchDialog.searchDlg != null)
			{
				SearchDialog.searchDlg.chbMatchCase.Checked = !SearchDialog.searchDlg.chbMatchCase.Checked;
			}
		}
		public virtual void ToggleRegularExpressions()
		{
			this.CheckOption(SearchOptions.RegularExpressions);
			if (SearchDialog.searchDlg != null)
			{
				SearchDialog.searchDlg.chbUseRegularExpressions.Checked = !SearchDialog.searchDlg.chbUseRegularExpressions.Checked;
			}
		}
		public void EnableRegularExpressions(bool enable)
		{
			SearchDialog.regularExpressionsEnabled = enable;
			if (SearchDialog.searchDlg != null)
			{
				SearchDialog.searchDlg.chbUseRegularExpressions.Enabled = enable;
			}
		}
		public void ShowRegularExpressions(bool show)
		{
			SearchDialog.regularExpressionsVisible = show;
			if (SearchDialog.searchDlg != null)
			{
				SearchDialog.searchDlg.chbUseRegularExpressions.Visible = show;
			}
		}
		public virtual void ToggleSearchUp()
		{
			this.CheckOption(SearchOptions.BackwardSearch);
			if (SearchDialog.searchDlg != null)
			{
				SearchDialog.searchDlg.chbSearchUp.Checked = !SearchDialog.searchDlg.chbSearchUp.Checked;
			}
		}
		public void TogglePromptOnReplace()
		{
			this.CheckOption(SearchOptions.PromptOnReplace);
			if (SearchDialog.searchDlg != null)
			{
				SearchDialog.searchDlg.chbPromptOnReplace.Checked = !SearchDialog.searchDlg.chbPromptOnReplace.Checked;
			}
		}
		public virtual void ToggleWholeWord()
		{
			this.CheckOption(SearchOptions.WholeWordsOnly);
			if (SearchDialog.searchDlg != null)
			{
				SearchDialog.searchDlg.chbMatchWholeWord.Checked = !SearchDialog.searchDlg.chbMatchWholeWord.Checked;
			}
		}
	}
}
